package cn.tedu.elo.petForum.service.impl;

import cn.tedu.elo.petForum.mapper.PostMapper;
import cn.tedu.elo.petForum.pojo.dto.PostParam;
import cn.tedu.elo.petForum.pojo.dto.PostQuery;
import cn.tedu.elo.petForum.pojo.entity.Post;
import cn.tedu.elo.petForum.pojo.vo.PostVo;
import cn.tedu.elo.petForum.service.ForumService;
import cn.tedu.elo.user.pojo.entity.User;
import cn.tedu.elo.user.pojo.vo.UserVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class ForumServiceImpl implements ForumService {
    @Autowired
    PostMapper postMapper;
    @Override
    public void savePost(PostParam postParam) {
        log.debug("保存帖子业务，参数：{}", postParam);
        Post post= new Post();
        BeanUtils.copyProperties(postParam, post);
        if (post.getId() == null) {//新增
            post.setStatus(1);
            post.setCreateTime(new Date());
            log.debug("帖子对象：{}", post);
            postMapper.insert(post);
        } else {//更新
            post.setUpdateTime(new Date());
            postMapper.update(post);
        }
    }

    @Override
    public List<PostVo> selectPost(PostQuery postQuery) {
        log.debug("查询帖子业务，参数：{}", postQuery);
        List<PostVo> list = postMapper.selectPost(postQuery);
        log.debug("查询出的列表：{}", list);
        return list;
    }

    @Override
    public void deletePost(Long id) {
        log.debug("删除帖子业务，参数：{}", id);
        postMapper.deleteById(id);
    }
}
